var path = '/';

(function($){

    $.ajaxSetup({
  async: true
  });

  $.cget = function(url,data,back){
    $.get(path+url,data,function(data){
      if(data.code==401){
         window.location.href='/';
      }
      if(!data.result){
              $.messager.alert('错误',data.msg);
            }
      back.call(this,data);
    });
  }
  $.cpost = function(url,data,back){
    $.post(path+url,data,function(data){
      if(data.code==401){
        window.location.href='/';
      }
      if(!data.result){
        $.messager.alert('错误',data.msg);
      }
      back.call(this,data);
    });
  };

  $.saveLogin = function(data){
    $.cookie('login',JSON.stringify(data));
  }

  $.loginOut = function(){
       $.cget('user/loginOut.do',{},function(){
            $.cookie('login',null);
            window.location.href = 'index.html'
       })

  };
  $.getLogin = function(){
    return JSON.parse($.cookie('login'));
  }

  $.changeCurrentUser = function(id){
        var loginObj = $.getLogin()
        $($.getLogin().correlationUsers).each(function(index,obj){
            if(obj.id==id){
                loginObj.currentUser = obj;
                return false;
            }

        });

        $.saveLogin(loginObj);
    }

    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

   $.myformatter = function (date){
      var y = date.getFullYear();
      var m = date.getMonth()+1;
      var d = date.getDate();
      return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    }
   $.myparser = function (s){
      if (!s) return new Date();
      var ss = (s.split('-'));
      var y = parseInt(ss[0],10);
      var m = parseInt(ss[1],10);
      var d = parseInt(ss[2],10);
      if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
          return new Date(y,m-1,d);
      } else {
          return new Date();
      }
   }
})($);
